//
//  CardbagListVC.swift
//  CardBag
//
//  Created by hl on 2019/5/22.
//  Copyright © 2019 hlznj. All rights reserved.
//

import UIKit

class CardbagListVC: BaseViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView: UITableView?
    var dataSource = CardbayManager.getCardbagGroup()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "卡包"

        self.addSubviews()

        NotificationCenter.default.addObserver(self, selector: #selector(reloadCollection), name: Notification.Name(UpdataNotifi), object: nil)
    }

    @objc func reloadCollection() {
        self.dataSource = CardbayManager.getCardbagGroup()
        self.tableView?.reloadData()
    }

    func addSubviews() {
        let rightItem1 = UIBarButtonItem.init(image: UIImage.init(named: "addbtn")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(addMoreGroupBtnAction))
        let rightItem2 = UIBarButtonItem.init(image: UIImage.init(named: "edit_icon")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(editBtnAction))
        self.navigationItem.rightBarButtonItems = [rightItem1,rightItem2]

        self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: kSafeAreaTopHeight, width: kScreenW, height: kScreenH-kSafeAreaTopHeight))
        self.tableView?.delegate = self as UITableViewDelegate
        self.tableView?.dataSource = self as UITableViewDataSource
        self.tableView?.tableFooterView = UIView.init()
        self.view.addSubview(self.tableView!)
        self.tableView?.register(AddCardbagListCell.self, forCellReuseIdentifier: "AddCardbagListCell")
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let group = self.dataSource[section]
        return group.groupName
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let group = self.dataSource[section]
        return group.cardbags.count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataSource.count
    }

    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        if sourceIndexPath.section == destinationIndexPath.section {
            let group = self.dataSource[sourceIndexPath.section]
            CardbayManager.moveCardBag(beginIndex: sourceIndexPath.row, endIndex: destinationIndexPath.row, group: group)
        } else {
            let sourceGroup = self.dataSource[sourceIndexPath.section]
            let cardbag = sourceGroup.cardbags[sourceIndexPath.row]
            let endGroup = self.dataSource[destinationIndexPath.section]
            CardbayManager.insertCardBag(index: destinationIndexPath.row, group: endGroup, cardbag: cardbag)
            CardbayManager.removeCardbag(index: sourceIndexPath.row, group: sourceGroup)
        }
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let group = self.dataSource[indexPath.section]
            CardbayManager.removeCardbag(index: indexPath.row, group: group)
        }
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let group = self.dataSource[indexPath.section]
        let cardbag: CardbagModel = group.cardbags[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "AddCardbagListCell", for: indexPath)
        cell.textLabel?.text = "    " + cardbag.cardbagName
        return cell
    }

    @objc func editBtnAction() {
        self.tableView!.isEditing = !self.tableView!.isEditing
        self.tableView!.reloadData()
    }

    @objc func addMoreGroupBtnAction() {
        let alert = UIAlertController.init(title: "添加分组", message: "", preferredStyle: .alert)
        alert.addTextField { (textField) in
            textField.placeholder = "请输入分组名字"
        }
        let action1 = UIAlertAction.init(title: "确定", style: .default) { (action) in
            let text = alert.textFields?.first?.text
            if text!.count > 0 {
                let groud = CardbagGroupModel.init()
                groud.groupName = text!
                CardbayManager.insertCardbagGroup(cardbagGroup: groud)
            }
        }
        let action2 = UIAlertAction.init(title: "取消", style: .cancel) { (action) in }
        alert.addAction(action1)
        alert.addAction(action2)
        self.present(alert, animated: true, completion: nil)
    }
}
